<template>
	<view class="addVisitorsBox">
		<view class="visitorsInfo">
			<view class="title">{{user.uid == uid?'个人中心':'用户信息'}}</view>
			
			<u-form v-if="form.type !=0" :model="form" ref="uForm" label-width="130">
				<u-form-item v-if="user.uid != uid" label="类型"><u-input :disabled="true" :value="uName[type]" /></u-form-item>
				<u-form-item label="昵称" ><u-input v-model="form.nickName" /></u-form-item>
				<u-form-item label="姓名"><u-input v-model="form.realName" /></u-form-item>
				<u-form-item label="职称" v-if="form.type==3"><u-input v-model="form.jobTitle" /></u-form-item>
				<u-form-item label="年龄"><u-input type="number" v-model="form.age" /></u-form-item>
				<u-form-item label="性别">
					
					<radio-group @change="radioChange">
						<label style="padding-right: 30rpx;" class="radio" v-for="(item, index) in items" :key="index">
							<radio :value="item.value" :checked="index === current" />{{item.name}}
						</label>
					</radio-group>
					
				</u-form-item>
				<u-form-item label="电话"><u-input v-model="form.phone" /></u-form-item>
				<u-form-item label="地址"><u-input v-model="form.address" /></u-form-item>
				<u-form-item label="修改密码" v-if="user.uid == uid"><u-input v-model="password" /></u-form-item>
				<u-form-item label="头像">
					<view class="u-flex u-direction-row u-p-l-25 u-p-b-25 ">
						          <u-image border-radius="15" @tap="change('pic')" class="u-image-shadow u-p-r-30" height="120rpx"  
							width="120rpx" :src="getPic(form.pic)" mode="aspectFill" >
					           		<view slot="error" style="font-size: 24rpx;" class="u-text-center">
						             	<u-icon size="30" name="plus"></u-icon><br />选择图片
						            </view>
					           		<view slot="loading" style="font-size: 24rpx;" class="u-text-center">
						             	<u-icon size="30" name="reload"></u-icon><br />加载中
						            </view>
									</u-image> 
								</view>		
				</u-form-item>
				
			</u-form>
			
		</view>
		
		<view class="addButton" @click="confirm">
			提交
		</view>
	</view>
</template>

<script>
	import appRequest from "@/common/appRequestUrl.js";
	import base64 from "@/common/base64.js"
	export default {
		data() {
			return {
				items: [{
					value: 0,
					name: '女',
					checked: 'true'
				},{
					value: 1,
					name: '男',
					checked: 'false'
				}],
				uName:['/','管理员','患者','医生'],
				type:2,
				uid:0,
				password:"",
				form:{
					nickName:"",
					realName:"",
					age:"",
					sex:"",
					phone:"",
					address:"",
					pic:"nuoMi.png",
					type:0,
					jobTitle:""
				},
				//默认建档方式
				filingWay: 0,
				//是否显示号码类型选择
				showCardType: false,
				//号码类型
				cardTypeList: [
					['就诊卡', '医保卡']
				],
				//绑卡类型
				cardType: null,
				//就诊人信息
				userInfo: {

				},
				picPath:appRequest.urlMap.getPic,
				current:0,
				user:{}
			}
		},
		onLoad:function(options){
			this.user = appRequest.getUserInfo();
			if(!this.user){
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			if(options.uid){
				this.uid = options.uid;
				this.getUser();
				
			}else if(options.type){
				this.type = options.type;
				this.form.type =this.type;
			}else{
				this.uid = this.user.uid;
				this.getUser();
			}
		},
		methods: {
			radioChange(evt){
				this.current = evt.detail.value;
				this.form.sex = evt.detail.value;
			},
			getPic(pic){
				if(pic.length > 100){
					return pic;
				}else{
					return this.picPath + pic;
				}
				
			},
			getUser(){
				let _this = this;
				appRequest.request({
					method: "POST",
					data: {
						uid:this.uid,
						validFLag:1
					},
					url: appRequest.urlMap.findSysUser,
					success: function(res) {
						if(res.data.code == 200){
							_this.form = res.data.data;
							_this.type = _this.form.type;
							_this.current = _this.form.sex;
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
			},
			confirm() {
				
				if(!this.form.nickName || 
				!this.form.realName||
				!this.form.age||
				!this.form.phone||
				!this.form.address){
					_this.$api.msg("请填写完整");
					return;
				}
				
				if(this.password){
					let base64en = new base64()
					this.form["password"] = base64en.encode(this.password);
				}
				
				let _this = this;
				appRequest.request({
					method: "POST",
					data: this.form,
					url: appRequest.urlMap.editSysUser,
					success: function(res) {
						if(res.data.code == 200){
							_this.$api.msg("提交成功");
							uni.navigateBack();
						}else{
							_this.$api.msg(res.data.msg);
						}
					},
					fail: function(res) {
						_this.$api.msg("请求异常");
					}
				})
			},
			change(name) {
				let _this = this;
				uni.chooseImage({
					count: 1,
					sizeType: ["compressed"],
					success: function(res) {
						let src = res.tempFiles[0].path;
						uni.compressImage({
							src: src,
							width: 450,
							quality: 70,
							success: function(path) {
								let filePath = path.tempFilePath;
								wx.getFileSystemManager().readFile({
									filePath: filePath,
									encoding: 'base64',
									fail: function(errMsg) {
										console.log(errMsg);
									},
									success: function(res) {
										_this.form[name] =
											"data:image/jpeg;base64," + res.data;
									}
								});
							}
						});
					},
					fail: function(errMsg) {
						console.log(errMsg);
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.addVisitorsBox {
		padding:30rpx 40rpx;
		.visitorsInfo{
			.title{
				font-weight: 400;
				line-height: 56rpx;
				color: #343434;
				font-size: 40rpx;
			}
			.value{
				margin-top: 30rpx;
			}
		}
		.filingWay{
			font-weight: 400;
			line-height: 56rpx;
			color: #343434;
			margin-top: 30rpx;
			font-size: 40rpx;
			.cardType{
				display: flex;
				justify-content: space-between;
				padding: 9rpx 6rpx;
				font-size: 30rpx;
				margin-top: 30rpx;
				color: #CBCBCB;
				border: 1px solid #E4E4E2;
				border-radius: 8rpx;
			}
			.value{
				margin-top: 30rpx;
			}
		}
		.addButton {
			width: 490rpx;
			height: 84rpx;
			margin: 100rpx auto 0;
			background: linear-gradient(90deg, #5088FD 0%, #62B9F9 100%);
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 40rpx;
			letter-spacing: 4rpx;
		}
	}
</style>
